package com.dman.multi.core;

import java.io.File;
import java.io.FilenameFilter;

public class GlobFilenameFilter implements FilenameFilter {
	
	private String glob;
	
	public GlobFilenameFilter(String glob) {
		this.glob = glob;
	}

	/**
	 * This only implements a simple globbing, they are as below.
	 * *file
	 * abc*
	 * *part*
	 * @param dir
	 * @param name
	 * @return if the filename has to be included
	 */
	@Override
	public boolean accept(File dir, String name) {
		if(glob == null)
			return true;
		int len = glob.length() - 1;
		String substr = glob.replace("*", "");
		if(glob.charAt(len) == '*' && glob.charAt(0) == '*')
			return name.indexOf(substr) >= 0;
		if(glob.charAt(glob.length() - 1) == '*')
			return name.startsWith(substr, 0);
		if(glob.charAt(0) == '*')
			return name.endsWith(substr);
		
		return false;
	}

}
